package com.fetch.spider;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.tinygroup.htmlparser.node.HtmlNode;
import org.tinygroup.parser.filter.FastNameFilter;
import org.tinygroup.tinyspider.Processor;

import com.download.download.MulThreadDownload;
import com.download.entity.DownloadInfo;
import com.download.utils.DownloadHelper;
import com.download.utils.LogUtil;

public class ImgProcessor2 implements Processor {

    static String path = "F:"+File.separator+"fetchGirl3";
    @Override
    public void process(String url, HtmlNode node, Map<String, Object> parameters) throws Exception {
        FastNameFilter<HtmlNode> filter = new FastNameFilter<HtmlNode>(node);
        filter.setNodeName("img");
        List<HtmlNode> imgList = filter.findNodeList();
        LogUtil.info("还有" + imgList.size() + "个妹子要下载！");
        String imgUrl = "";
        for (HtmlNode img : imgList) {
//            if(img != imgList.get(0)) break;
            if(img.getAttribute("src") != null && img.getAttribute("src").indexOf("editor") != -1){
                imgUrl = img.getAttribute("src").indexOf(FetchSpider.baseUrl) != -1 ?  img.getAttribute("src") : FetchSpider.baseUrl + img.getAttribute("src");
            } else {
                if(img.getAttribute("data-original").indexOf(FetchSpider.baseUrl) != -1){
                    imgUrl = img.getAttribute("data-original");
                }else{
                    imgUrl = FetchSpider.baseUrl + img.getAttribute("data-original");
                }
            } 
            downLoadImg(imgUrl,2);
        }
    }
    
    public void downLoadImg(String imgUrl, int threadCount){
        DownloadInfo downloadInfo = DownloadHelper.createDownloadInfo(imgUrl,null, path, threadCount);
        MulThreadDownload mulThreadDownload = new MulThreadDownload(downloadInfo);
        new Thread(mulThreadDownload).start();
    }
}
